package com.hugehard.here;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.util.Log;

import java.sql.Date;
import java.util.ArrayList;

/**
 * manage to send, receive or get message
 * @author wingyip
 *
 */
public class MessageManager {

	/**
	 * represent the type of the message, sent, receive, none(a default type)
	 * @author wingyip
	 *
	 */
	public enum MessageType {
		SENT, RECEIVE, NONE
	};

	private final static String TAG = MessageManager.class.getSimpleName();
	private Context mContext = null;

	/**
	 * constructor
	 * @param context
	 */
	public MessageManager(Context context) {
		mContext = context;
	}


	/**
	 * get all the message from phone, including inbox, sent and draft
	 * @return a arraylist of the message
	 */
	public ArrayList<PhoneMessage> getAllMessage() {
		ArrayList<PhoneMessage> msgList = new ArrayList<MessageManager.PhoneMessage>();

		final String SMS_URI_ALL = "content://sms/";
		// final String SMS_URI_INBOX = "content://sms/inbox";
		// final String SMS_URI_SENT = "content://sms/sent";
		// final String SMS_URI_DRAFT = "content://sms/draft";
		try {
			ContentResolver cr = mContext.getContentResolver();
			String[] projection = new String[] { "_id", "address", "person",
					"body", "date", "type" };
			Uri uri = Uri.parse(SMS_URI_ALL);
			Cursor cur = cr.query(uri, projection, null, null, "date desc");

			if (cur.moveToFirst()) {
				MessageType type = MessageType.NONE;
				String phoneNumber = "";
				String content = "";
				Date date = null;

				int phoneNumberColumn = cur.getColumnIndex("address");
				int smsbodyColumn = cur.getColumnIndex("body");
				int dateColumn = cur.getColumnIndex("date");
				int typeColumn = cur.getColumnIndex("type");

				do {
					int typeId = cur.getInt(typeColumn);
					if (typeId == 1) {
						type = MessageType.RECEIVE;
					} else if (typeId == 2) {
						type = MessageType.SENT;
					}
					phoneNumber = cur.getString(phoneNumberColumn);
					content = cur.getString(smsbodyColumn);
					date = new Date(Long.parseLong(cur.getString(dateColumn)));

					PhoneMessage phoneMsg = new PhoneMessage(type, phoneNumber,
							content, date);
					msgList.add(phoneMsg);
				} while (cur.moveToNext());
			}
		} catch (SQLiteException ex) {
			Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
		}

		return msgList;
	}

	public static class PhoneMessage {
		MessageType mType = MessageType.NONE;
		String mSentPhoneNumber = "";
		String mContent = "";
		Date mDate = null;

		/**
		 * constructor
		 * @param type type of the message
		 * @param sentPhoneNumber the phone number of the message sender
		 * @param content the content of the message
		 * @param date the date of the message
		 */
		public PhoneMessage(MessageType type, String sentPhoneNumber,
				String content, Date date) {
			mType = type;
			mSentPhoneNumber = sentPhoneNumber;
			mContent = content;
			mDate = date;
		}

		/**
		 * get the message type(sent, receive, none)
		 * @return MessageType
		 */
		public MessageType getMessageType() {
			return mType;
		}

		/**
		 * get the content of the message
		 * @return String
		 */
		public String getContent() {
			return mContent;
		}

		/**
		 * get the phone number of the message sender
		 * @return String
		 */
		public String getSentPhoneNumber() {
			return mSentPhoneNumber;
		}

		/**
		 * get the date of the message
		 * @return sql.Date
		 */
		public Date getDate() {
			return mDate;
		}
	}
}
